<?php

namespace App\Admin\Forms\Deposit;

use App\Models\UserDeposit;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class DepositSuccessForm extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        /**
         * @var $user_deposit UserDeposit
         */
        $user_deposit = UserDeposit::query()->find($this->payload['id']);
        $to_real_amount = floatval($input['to_real_amount'] ?? 0);
        $op_result = $user_deposit->success($to_real_amount);
        if ($op_result) {
            return $this
                ->response()
                ->success('执行成功')
                ->refresh();
        } else {
            return $this
                ->response()
                ->error('执行失败');
        }
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        /**
         * @var $user_deposit UserDeposit
         */
        $user_deposit = UserDeposit::query()->find($this->payload['id']);
        $to_real_amount = $user_deposit->to_real_amount;
        if ($to_real_amount <= 0) {
            $to_real_amount = $user_deposit->to_amount;
        }
        $this->text('to_real_amount', __("实际到账"))->value(asMoney($to_real_amount));
        $this->hidden("id")->value($this->getKey());
    }

    public function default()
    {
        return [
        ];
    }
}
